/**
 * Created by 王健 on 2017/8/31.
 */

$(function () {
    var live_data = {
        baseConfig: baseConfig,
        live_program_id:GetQueryString("program_id"),
        type:GetQueryString("type"),
        prgoram: {},
        show_img: true,
        goods_list: [],
        cart_list: [],
        goods_ids: [],
        is_all: false,
        goods_order_list: [],
    };
    if(!live_data.type){
        live_data.type = 'android'
    }
    var a = document.getElementById("live_id");
    if(a){
        a .value = 'live_id:'+ live_data.live_program_id;
    }

    window.order_detail = new Vue({
        el: '#play_page',
        data: live_data,
        methods:{
            price_filter: price_filter,
            timeline_str: function () {
                var t = new Date().getTime();
                t = parseInt(t/1000/60/3);
                return "?t="+t;
            },
            open_order_info: function (order_id) {
                location.href = 'user_goods_orders.html?goods_order_id=' + order_id;
            },
            go_goods_detail:function (goods_id) {
                location.href = 'goods.html?goods_id='+goods_id
            },
            cart_pay: function (goods_ids) {
                var cart_list_ids = [];
                $.each(goods_ids, function (index, goods_id) {
                    $.each(live_data.cart_list, function (index, c_goods) {
                        if(c_goods.goods_id == goods_id){
                            cart_list_ids.push(c_goods.id)
                        }
                    })
                })
                api.lorder.create_order({cart_list:cart_list_ids.join(',')}).then(function (data) {
                    var payorder_id = data.result.id;
                    location.href = './pay.html?payorder_id='+payorder_id
                })
            },
            pop_num: function (c_goods) {
                c_goods.num -= 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                })
            },
            add_num: function (c_goods) {
                c_goods.num += 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                })
            },
            checkAll: function () {
                live_data.goods_ids.splice(0, live_data.goods_ids.length);
                if (!live_data.is_all) {
                    live_data.cart_list.forEach(function (item) {
                        live_data.goods_ids.push(item.goods_id);
                    });
                }
                live_data.is_all = !live_data.is_all;
            },
            price_all: function (goods_ids) {
                var prices = 0;
                $.each(goods_ids, function (index, goods_id) {
                    $.each(live_data.cart_list, function (index, c_goods) {
                        if (c_goods.goods_id == goods_id) {
                            prices += c_goods.goods__price * c_goods.num
                        }
                    });
                });
                return live_data.cart_list.prices = prices
            },
            testPlay: function () {
                if(live_data.show_img){
                    var play_background = document.getElementById("play_background");
                    play_background.style.top = '40px';
                    window.test_video_ios.play();
                }else{
                    window.test_video_ios.stop();
                }


                live_data.show_img = !live_data.show_img;
            }
        }
    });

    live_data.sessionid = GetQueryString('sessionid');
    if (live_data.sessionid) {
        live_data.sessionid_q = "sessionid=" + live_data.sessionid;
        client_storage('sessionid', live_data.sessionid);
    }

    api.lprogram.get_live_program({live_program_id: live_data.live_program_id}).then(function (data) {
        live_data.prgoram = data.result;
        // live_data.prgoram.current_config__data_json = JSON.parse(data.result.current_config__data_json);
        init_video(live_data.prgoram);
    });

    // api.lgoods.query_cart_list({live_program_id:live_data.live_program_id}).then(function (data) {
    //     live_data.cart_list = data.result.list
    // });
    //
    // api.lgoods.query_goods_by_live_program_list({live_program_id:live_data.live_program_id,status:0}).then(function (data) {
    //     live_data.goods_list = data.result.list
    // });
    //
    // api.lorder.query_maiuser_order_by_programe_list({live_program_id:live_data.live_program_id}).then(function (data) {
    //     live_data.goods_order_list = data.result.list
    //     _(data.result.list).each(function (item) {
    //         live_data.goods_order_list.push(item);
    //         item['goods_num'] = 0;
    //         _(item.goods_order_item_list).each(function (goods) {
    //             item['goods_num'] += goods.num;
    //         });
    //     });
    // });

    login_mogu(function () {

        api.lgoods.query_cart_list({live_program_id:live_data.live_program_id}).then(function (data) {
            live_data.cart_list = data.result.list
        });

        api.lgoods.query_goods_by_live_program_list({live_program_id:live_data.live_program_id,status:0}).then(function (data) {
            live_data.goods_list = data.result.list
        });

        api.lorder.query_maiuser_order_by_programe_list({live_program_id:live_data.live_program_id}).then(function (data) {
            live_data.goods_order_list = data.result.list
            _(data.result.list).each(function (item) {
                live_data.goods_order_list.push(item);
                item['goods_num'] = 0;
                _(item.goods_order_item_list).each(function (goods) {
                    item['goods_num'] += goods.num;
                });
            });
        });
    }, live_data.type, live_data.live_program_id);


});